package com.wzq.permission.center.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wzq.permission.center.common.DataPermissionConvertUtil;
import com.wzq.permission.center.mapper.ApplicationMapper;
import com.wzq.permission.center.mapper.ApplicationUserMapper;
import com.wzq.permission.center.mapper.DataPermissionMapper;
import com.wzq.permission.center.mapper.OperateLogMapper;
import com.wzq.permission.center.mapper.RoleMapper;
import com.wzq.permission.center.mapper.UserMapper;
import com.wzq.permission.center.mapper.UserOrganizeMapper;
import com.wzq.permission.center.mapper.UserRoleMapper;
import com.wzq.permission.client.model.LogTypeEnum;
import com.wzq.permission.client.model.Pager;
import com.wzq.permission.client.model.admin.ApplicationModel;
import com.wzq.permission.client.model.admin.ApplicationUserModel;
import com.wzq.permission.client.model.admin.DataPermissionModel;
import com.wzq.permission.client.model.admin.OperateLogModel;
import com.wzq.permission.client.model.admin.RoleModel;
import com.wzq.permission.client.model.admin.UserModel;
import com.wzq.permission.client.model.admin.UserRoleModel;
import com.wzq.permission.client.service.UserService;

/**
 * 用户服务
 * Project:wzq-permission-center 
 * @Author chenjianfei
 * create Time:2015年12月31日 10：21：30
 *
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
	@Autowired
    private UserMapper mapper;
	@Autowired
	private ApplicationUserMapper applicationUserMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;
	@Autowired
    private RoleMapper roleMapper;
	@Autowired
	private UserOrganizeMapper userOrganizeMapper;
	@Autowired
	private DataPermissionMapper dataPermissionMapper;
	@Autowired
	private OperateLogMapper operateLogMapper;
	@Autowired
	private ApplicationMapper applicationMapper;
	@Override
	public void add(UserModel user) {
		Date now = new Date();
		user.setCreateTime(now);
		user.setUpdateTime(now);
		user.setLocked("0");
		user.setDeleted("0");
		mapper.add(user);
	}

	@Override
	public void update(UserModel user) {
		mapper.update(user);
	}

	@Override
	public void updateBySelective(UserModel user) {
		mapper.updateBySelective(user);
	}

	@Override
	public void delete(Object id) {
		//删除用户关联的系统
		applicationUserMapper.deleteByUserId((Long)id);
		//删除用户关联的角色
		userRoleMapper.deleteByUserAndAppId((Long)id, null);
		mapper.delete(id);
	}

	@Override
	public int queryByCount(UserModel user) {
		return mapper.queryByCount(user);
	}

	@Override
	public List<UserModel> queryList(UserModel user,Pager pager) {
		if(StringUtils.isNotEmpty(user.getUserName())){
			user.setUserName(user.getUserName()+"%");
		}
		return mapper.queryList(user,pager);
	}

	@Override
	public UserModel queryById(Object id) {
		return mapper.queryById(id);
	}

	@Override
	public UserModel login(UserModel user,String appKey) {
	    UserModel userModel = mapper.login(user,appKey);
	    if(userModel!=null){
	        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        ApplicationModel applicationModel = applicationMapper.getApplicationByKey(appKey);
	        OperateLogModel operateLogModel = new OperateLogModel();
	        operateLogModel.setLogType(LogTypeEnum.LOGIN.getType());
	        if(applicationModel!=null){
	            operateLogModel.setApplicationId(applicationModel.getApplicationId().intValue());
	            operateLogModel.setApplicationName(applicationModel.getApplicationName());
	        }
	        operateLogModel.setOperateModule(LogTypeEnum.LOGIN.getDesc());
	        operateLogModel.setIp(user.getIp());
	        operateLogModel.setUserId(userModel.getUserId().intValue());
	        operateLogModel.setUserName(userModel.getUserName());
	        //operateLogModel.setLogDesc(format.format(new Date())+"登录["+applicationModel.getApplicationName()+"]");
	        operateLogModel.setLogDesc("登录["+applicationModel.getApplicationName()+"]");
	        operateLogMapper.add(operateLogModel);
	    }
		return userModel;
	}

	@Override
	public int logicDelete(Long[] userIds) {
		for(Long userId:userIds){
			//删除用户关联的系统
			applicationUserMapper.deleteByUserId(userId);
			//删除用户关联的角色
			userRoleMapper.deleteByUserAndAppId(userId, null);
			//根据用户删除组织管理
			userOrganizeMapper.deleteUserOrgByUserId(userId);
		}
		return mapper.logicDelete(userIds);
	}

	@Override
	public void allotApplication(Long userId,
			List<ApplicationUserModel> applicationUserModelList) {
		applicationUserMapper.deleteByUserId(userId);
		if(applicationUserModelList != null && applicationUserModelList.size()>0){
			applicationUserMapper.addBatch(applicationUserModelList);
		}
	}

	@Override
	public List<ApplicationUserModel> queryUserApplication(Long userId) {
		return applicationUserMapper.queryUserApplication(userId);
	}

	@Override
	public void allotRole(Long userId, Long applicationId,
			List<UserRoleModel> userRoleModelList) {
		userRoleMapper.deleteByUserAndAppId(userId, applicationId);
		if(userRoleModelList != null && userRoleModelList.size() > 0){
			userRoleMapper.addBatch(userRoleModelList);
		}
	}

	@Override
	public List<UserRoleModel> queryUserRole(Long userId, Long applicationId) {
		return userRoleMapper.queryUserRole(userId, applicationId);
	}

	@Override
	public int isExitUserName(String userName) {
		return mapper.isExitUserName(userName);
	}

    @Override
    public List<Map> queryDataPermissionByUserId(Long userId, String applicationId) {
        List<DataPermissionModel> dataPermissionModelList = dataPermissionMapper.queryDataPermissionByUserId(userId, applicationId);
        return DataPermissionConvertUtil.convertToDataPermissionVO(dataPermissionModelList);
    }

    @Override
    public List<RoleModel> getRoleByUserId(Long userId, String applicationId) {
        return roleMapper.queryRoleByUserId(userId, applicationId);
    }

	@Override
	public void loginOut(UserModel user,String appKey) {
		 if(user!=null){
	        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        ApplicationModel applicationModel = applicationMapper.getApplicationByKey(appKey);
	        OperateLogModel operateLogModel = new OperateLogModel();
	        operateLogModel.setLogType(LogTypeEnum.LOGIN_OUT.getType());
	        if(applicationModel!=null){
	            operateLogModel.setApplicationId(applicationModel.getApplicationId().intValue());
	            operateLogModel.setApplicationName(applicationModel.getApplicationName());
	        }
	        operateLogModel.setOperateModule(LogTypeEnum.LOGIN_OUT.getDesc());
	        operateLogModel.setIp(user.getIp());
	        operateLogModel.setUserId(user.getUserId().intValue());
	        operateLogModel.setUserName(user.getUserName());
	        //operateLogModel.setLogDesc(format.format(new Date())+"登录["+applicationModel.getApplicationName()+"]");
	        operateLogModel.setLogDesc("退出["+applicationModel.getApplicationName()+"]");
	        operateLogMapper.add(operateLogModel);
		 }
		
	}

}
